package com.baidu.encrypt.handler;

import com.baidu.encrypt.common.utils.SM4Util;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baidu.encrypt.common.utils.SM3Util;
import com.baidu.encrypt.entity.SampleInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.StringJoiner;

@Component
public class SampleInfoEncryptHandler implements MetaObjectHandler {

    @Autowired
    private SM4Util sm4Util;
    
    @Override
    public void insertFill(MetaObject metaObject) {
        Object entity = metaObject.getOriginalObject();
        if (entity instanceof SampleInfo) {
            SampleInfo sample = (SampleInfo) entity;

            try {
                // 对每个字段（除了id, sm3）进行SM4加密
                sample.setName(sm4Util.encrypt(sample.getName()));
                sample.setSource(sm4Util.encrypt(sample.getSource()));
                sample.setTags(sm4Util.encrypt(sample.getTags()));
                sample.setVersion(sm4Util.encrypt(sample.getVersion()));
                sample.setStoragePath(sm4Util.encrypt(sample.getStoragePath()));
                sample.setFileName(sm4Util.encrypt(sample.getFileName()));
                sample.setFileSize(sm4Util.encrypt(sample.getFileSize()));
                sample.setFileType(sm4Util.encrypt(sample.getFileType()));
                sample.setCreatedBy(sm4Util.encrypt(sample.getCreatedBy()));
                sample.setUpdatedBy(sm4Util.encrypt(sample.getUpdatedBy()));
                sample.setProjectId(sm4Util.encrypt(sample.getProjectId()));

                // SM3摘要（用于防篡改）
                String sm3Plain = buildPlainString(sample);
                sample.setSm3(SM3Util.encrypt(sm3Plain));

            } catch (Exception e) {
                throw new RuntimeException("加密失败", e);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        insertFill(metaObject); // 更新时同样执行加密
    }

    /** 拼接明文用于SM3摘要计算 */
    private String buildPlainString(SampleInfo sample) throws Exception {
        StringJoiner joiner = new StringJoiner("|");
        joiner.add(sample.getName() == null ? "" : sample.getName());
        joiner.add(sample.getSource() == null ? "" : sample.getSource());
        joiner.add(sample.getTags() == null ? "" : sample.getTags());
        joiner.add(sample.getVersion() == null ? "" : sample.getVersion());
        joiner.add(sample.getStoragePath() == null ? "" : sample.getStoragePath());
        joiner.add(sample.getFileName() == null ? "" : sample.getFileName());
        joiner.add(sample.getFileSize() == null ? "" : sample.getFileSize());
        joiner.add(sample.getFileType() == null ? "" : sample.getFileType());
        joiner.add(sample.getCreatedBy() == null ? "" : sample.getCreatedBy());
        joiner.add(sample.getUpdatedBy() == null ? "" : sample.getUpdatedBy());
        joiner.add(sample.getProjectId() == null ? "" : sample.getProjectId());
        return joiner.toString();
    }
}
